<template>
	<view class="content" >
		<view class="status_bar">     
		 </view>
		<view class="navTabBox">
			<view class="longTab">
				<scroll-view scroll-x="true" style="white-space: nowrap; display: flex;width: 100%;" scroll-with-animation>
					<view class="longItem" style="width:20%" v-for="(item,index) in tabTitle" :key="index">
						<view class="" :data-index="index"  :id="'id'+index" @click="longClick(item.id)">{{item.name}}</view>
						<view class="underlineBox">
							<view class="underline" :class="active == item.id ? 'bgcolor' :''"></view>
						</view>
					</view>
				</scroll-view>
			</view> 
		</view>
		
		<view class="video" >
			<view class="video_item" v-for="(item, index) in videoList" :key="index" @click="videoDetial(item.id,item.fee,item.cate_id)"  >
				<!-- <image src="../../static/yaoqing.png"></image> -->
				<image :src="ajax + item.image"></image>
				<view class="video_text">{{item.name}}<text class="more iconfont icon-ziyuan"></text></view>
			</view>
		</view>
		<uni-load-more :loadingType="loadingType" :contentText="contentText"  v-if="videoList.length != 0"></uni-load-more>
		 
		<view class="wu" v-if="videoList.length == 0">暂无数据~</view> 
	</view>
</template>

<script>
import getdata from '@/common/ajax.js';	
import uniLoadMore from '@/components/uni-load-more.vue';
var
	page = 1,
	timer = null,
	pages = 1;
	export default {
		components: {uniLoadMore},
		data() {
			return {
				tabTitle: [],
				active: 1,
				videoList:[],
				loadingText: '加载中...',
				loadingType: 0, //定义加载方式 0---contentdown  1---contentrefresh 2---contentnomore
				contentText: {
					contentdown: '上拉显示更多',
					contentrefresh: '正在加载...',
					contentnomore: '我也是有底线的~',
				},
				ajax:''
			}
		},
		onPullDownRefresh: function() {
			//下拉刷新
			this.longClick(this.active)
		},
		onReachBottom: function() {
			//上拉加载
			if (page <= pages) {
					this.getmore(this.active);
			}else{
					this.loadingType = 2
			}
		},
		onLoad() {
			this.classify()
			this.ajax = getdata.imgUrl
			console.log(this.ajax)
		},
		onShow(){
			this.userinfo()	
		},
		methods: { 
			userinfo(){
				//获取用户信息
				var _this = this
				var token = uni.getStorageSync('token')
				uni.request({
					url: getdata.websiteUrl + '/user/getUserInfo',
					header:{'content-type': "application/x-www-form-urlencoded",token:token},
					dataType: 'json',
					method: 'POST',
					success: res => {
						if(res.data.code == 1){
							_this.info = res.data.data
						}else if(res.data.code == 401){
							getdata.shou('请登录')
							uni.reLaunch({
							    url: '../login/login'
							});
						}else{
							getdata.shou(res.data.msg)
						}
					},
					fail: res => {
						getdata.shou('服务器断开链接')
					}
				});
			},
			classify(){
				//小说分类
				uni.showLoading({
				    title: '加载中'
				});
				var _this = this
				var token = uni.getStorageSync('token')
				uni.request({
					url: getdata.websiteUrl + '/novel/novel_cate',
					header:{'content-type': "application/x-www-form-urlencoded",token:token},
					dataType: 'json',
					method: 'POST',
					success: res => {
						uni.hideLoading();
						if(res.data.code == 1){
							_this.tabTitle = res.data.data
							let xiaoshuoid = _this.tabTitle[0].id
							//得到小说分类后首次请求小说列表
							this.classlist(xiaoshuoid)
						}else if(res.data.code == 401){
							getdata.shou('请登录')
							uni.reLaunch({
							    url: '../login/login'
							});
						}else{
							getdata.shou(res.data.msg)
						}
					},
					fail: res => {
						uni.hideLoading();
						getdata.shou('服务器断开链接')
					}
				});
			}, 
			classlist(id){
				//小说列表
				uni.showLoading({
				    title: '加载中'
				});
				var _this = this
				var token = uni.getStorageSync('token')
				page = 1
				var data = {
					cate_id:id,
					page:page,
					limit:10
				}
				uni.request({
					url: getdata.websiteUrl + '/novel/lists',
					header:{'content-type': "application/x-www-form-urlencoded",token:token},
					dataType: 'json',
					method: 'POST',
					data:data,
					success: res => {
						console.log(res,'lujing')
						uni.hideLoading();
						if(res.data.code == 1){
							uni.stopPullDownRefresh()
							_this.videoList = []
							_this.videoList = res.data.data.list
							// console.log(_this.videoList)
							pages = res.data.data.count
							page++
							if (page > pages) {
							}else{
								this.loadingType = 2
							}
						}else{
							getdata.shou(res.data.msg)
						}
					},
					fail: res => {
						uni.hideLoading();
						getdata.shou('服务器断开链接')
					}
				});
			},
			longClick(id) { 
				//切换时重新加载小说列表
				this.active = id
				this.classlist(id)
				uni.stopPullDownRefresh(); //得到数据后停止下拉刷新
			}, 
			getmore(id){
				var _this = this
				var token = uni.getStorageSync('token')
				var data = {
					cate_id:id,
					page:page,
					limit:10
				}
				_this.loadingType = 1 //正在加载
				uni.request({
					url: getdata.websiteUrl + '/novel/lists',
					header:{'content-type': "application/x-www-form-urlencoded",token:token},
					dataType: 'json',
					method: 'POST',
					data:data,
					success: res => {
						console.log(res,'')
						if(res.data.code == 1){
							_this.videoList =_this.videoList.concat(res.data.data.list) 
							page++
							uni.stopPullDownRefresh(); //得到数据后停止下拉刷新
						}else{
							getdata.shou(res.data.msg)
						}
					},
					fail: res => {
						getdata.shou('服务器断开链接')
					}
				});
			},
			videoDetial(id,fee){
				// console.log(this.info,this.info.money,this.info.level,fee)
				if(this.info.money<fee && this.info.user_level != 4){
					getdata.shou('余额不足，请充值')
				}else{
					uni.navigateTo({
						url:'xslist/xslist?id=' + id 
					})
				}
			}
		}
	}
</script>

<style lang="scss">
@import '../../static/iconfont/iconfont.css';	
.status_bar {
        height: var(--status-bar-height);
        width: 100%;
    }	
.bgcolor {
	background: #1578F2;
}

.color {
	// color: #1578F2 !important;
}
.wu{
	text-align: center;
	font-size: 30rpx;
	color: #8D8D97;
}
.video{
	width: 100%;
	box-sizing: border-box;	
	display: flex;
	justify-content: space-between;
	flex-wrap: wrap;
	padding:30rpx;
	.video_item{
		width: 342rpx;
		height: 430rpx;
		border-radius: 10rpx;
		// background-color: #007AFF;
		margin-bottom: 10rpx;
		position: relative;
		image{
			width: 100%;
			height: 100%;
			border-radius: 10rpx;
		}
		.video_text{
			width: 342rpx;
			height: 90rpx;
			line-height: 90rpx;
			border-radius: 0px 0px 10rpx 10rpx;
			box-sizing: border-box;	
			padding-left:40rpx;
			padding-right:20rpx;
			font-size: 24rpx;
			font-weight: bold;
			color: #FFFFFF;
			position: absolute;
			bottom:0;
			left:0;
			overflow: hidden;
			text-overflow:ellipsis;
			white-space: nowrap;
			.more{
				position: absolute;
				bottom:0;
				right:0;
			}
		}
	}
}
// 导航切换
.navTabBox {
		width: 100vw;
		color: black;
		font-size: 30rpx;

		.click {
			color: #2CB5A9;
		}

		.longTab {
			width: 100%;
			box-sizing: border-box;
			font-size: 28rpx;
			.longItem {
				width: 100%;
				height: 70upx;
				display: inline-block;
				line-height: 70upx;
				text-align: center;
				margin-top: 10rpx;
				box-sizing: border-box;
				padding: 0 30rpx;
				.underlineBox {
					height: 4rpx;
					width: 60%;
					margin: 0 auto;
					display: flex;
					align-content: center;
					justify-content: center;
					transition: .5s;

					.underline {
						width: 84upx;
						height: 4px;

					}
				}
			}

		}
	}	
</style>
